package com.ysq.excavator.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionSupport;
import com.ysq.excavator.entity.LuceneField;
import com.ysq.excavator.entity.QueryAttribute;
import com.ysq.excavator.service.LuceneService;
import com.ysq.excavator.util.CommonUtil;

@Controller("luceneAction")
@Scope("prototype")
public class LuceneAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	
	@Resource(name = "luceneServiceImpl")
	protected LuceneService luceneService;
	
	private String indexPath;
	private int page;
	private int rows;
	private String contentid;
	private List<Long> contentidList = new ArrayList<Long>();
	private List<LuceneField> docFieldList = new ArrayList<LuceneField>();//新增时，field集合
	private QueryAttribute queryAttr;//查询时

	public String getIndexPath() {
		return indexPath;
	}
	public void setIndexPath(String indexPath) {
		this.indexPath = indexPath;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public List<Long> getContentidList() {
		return contentidList;
	}
	public void setContentidList(List<Long> contentidList) {
		this.contentidList = contentidList;
	}
	public List<LuceneField> getDocFieldList() {
		return docFieldList;
	}
	public void setDocFieldList(List<LuceneField> docFieldList) {
		this.docFieldList = docFieldList;
	}
	public String getContentid() {
		return contentid;
	}
	public void setContentid(String contentid) {
		this.contentid = contentid;
	}
	public QueryAttribute getQueryAttr() {
		return queryAttr;
	}
	public void setQueryAttr(QueryAttribute queryAttr) {
		this.queryAttr = queryAttr;
	}
	public void luceneTest(){
		System.out.println("");
		
		System.out.println("lucene Test.....");
		
	}
	
	/**
	 * 查询lucene 所有数据跳转
	 * @return
	 */
	public String showAllData(){
		return SUCCESS;
	}
	
	/**
	 * 查询 lucene 所有数据
	 */
	public void findAllDocuments(){
		int start = (page-1)*rows;
		
		String jsonResult = luceneService.findAllDocuments(queryAttr,rows,start,indexPath);
		
		CommonUtil.printResult(jsonResult);
	}
	
	/**
	 * 获得树
	 */
	public void loadTree(){
		
		String jsonResult = luceneService.getLuceneTreeJson();
		CommonUtil.printResult(jsonResult);
	}
	
	/**
	 * 删除lucene
	 */
	public void luceneDelete(){
		String jsonResult = luceneService.deleteIndexById(indexPath,contentidList);
		
		CommonUtil.printResult(jsonResult);
	}
	
	/**
	 * 新增一条document
	 */
	public void insertOneDocument(){
		String jsonResult = luceneService.insertOneDocument(indexPath,docFieldList);
		CommonUtil.printResult(jsonResult);
	}
	
	/**
	 * 通过 contentid 获得数据
	 */
	public void getDataById(){
		String jsonResult = luceneService.getDataById(indexPath,contentid);
		
		CommonUtil.printResult(jsonResult);
	}
	
	/**
	 * 更新数据
	 */
	public void updateOneDocument(){
		String jsonResult = luceneService.updateOneDocument(indexPath,docFieldList);
		CommonUtil.printResult(jsonResult);
	}
	
}
